package com.lanbu.system.service.impl;

import java.sql.Time;
import java.util.List;

import com.lanbu.common.core.domain.entity.SysUser;
import com.lanbu.common.utils.DateUtils;
import com.lanbu.common.utils.SecurityUtils;
import com.lanbu.system.domain.ZntModuleScore;
import com.lanbu.system.domain.ZntStepScore;
import com.lanbu.system.domain.dto.EvaluationRequest;
import com.lanbu.system.domain.dto.EvaluationResponse;
import com.lanbu.system.domain.dto.ModuleScoreDTO;
import com.lanbu.system.domain.dto.StepScoreDTO;
import com.lanbu.system.mapper.ZntModuleScoreMapper;
import com.lanbu.system.mapper.ZntStepScoreMapper;
import com.lanbu.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lanbu.system.mapper.ZntEvaluationMapper;
import com.lanbu.system.domain.ZntEvaluation;
import com.lanbu.system.service.IZntEvaluationService;
import org.springframework.transaction.annotation.Transactional;
import java.security.SecureRandom;
import java.util.Base64;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.lanbu.system.domain.mongo.MongoMyUserOutlinksids;
import com.lanbu.system.service.impl.MongoMyUserOutlinksidsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.lanbu.common.annotation.Log;
import com.lanbu.common.core.controller.BaseController;
import com.lanbu.common.core.domain.AjaxResult;
import com.lanbu.common.core.domain.entity.SysDept;
import com.lanbu.common.core.domain.entity.SysRole;
import com.lanbu.common.core.domain.entity.SysUser;
import com.lanbu.common.core.page.TableDataInfo;
import com.lanbu.common.enums.BusinessType;
import com.lanbu.common.utils.SecurityUtils;
import com.lanbu.common.utils.StringUtils;
import com.lanbu.common.utils.poi.ExcelUtil;
import com.lanbu.system.service.ISysDeptService;
import com.lanbu.system.service.ISysPostService;
import com.lanbu.system.service.ISysRoleService;
import com.lanbu.system.service.ISysUserService;

/**
 * 用户评估记录Service业务层处理
 *
 * @author lanbu
 * @date 2025-04-24
 */
@Service
public class ZntEvaluationServiceImpl implements IZntEvaluationService
{


    @Autowired
    private ZntEvaluationMapper zntEvaluationMapper;
    @Autowired
    private ZntModuleScoreMapper zntModuleScoreMapper;
    @Autowired
    private ZntStepScoreMapper zntStepScoreMapper;
    @Override
    public ZntEvaluation selectZntEvaluationById(Long id)
    {
        return zntEvaluationMapper.selectZntEvaluationById(id);
    }

    @Override
    public List<ZntEvaluation> selectZntEvaluationList(ZntEvaluation zntEvaluation)
    {
        return zntEvaluationMapper.selectZntEvaluationList(zntEvaluation);
    }

    @Override
    public int insertZntEvaluation(ZntEvaluation zntEvaluation)
    {
        zntEvaluation.setCreateTime(DateUtils.getNowDate());
        return zntEvaluationMapper.insertZntEvaluation(zntEvaluation);
    }

    @Override
    public int updateZntEvaluation(ZntEvaluation zntEvaluation)
    {
        zntEvaluation.setUpdateTime(DateUtils.getNowDate());
        return zntEvaluationMapper.updateZntEvaluation(zntEvaluation);
    }

    @Override
    public int deleteZntEvaluationByIds(Long[] ids)
    {
        return zntEvaluationMapper.deleteZntEvaluationByIds(ids);
    }

    @Override
    public int deleteZntEvaluationById(Long id)
    {
        return zntEvaluationMapper.deleteZntEvaluationById(id);
    }

    @Override
    @Transactional
    public void saveEvaluation(EvaluationRequest request) {







        // 1. 插入主表 znt_evaluation
        ZntEvaluation evaluation = new ZntEvaluation();
        evaluation.setUserId(request.getUserId());
        evaluation.setTotalScore(request.getTotalScore());
        evaluation.setUsedTime(Time.valueOf(request.getUsedTime()));
        zntEvaluationMapper.insertZntEvaluation(evaluation);

        Long evalId = evaluation.getId(); // 获取自增主键

        // 2. 插入模块得分表
        for (ModuleScoreDTO module : request.getModuleScores()) {
            ZntModuleScore ms = new ZntModuleScore();
            ms.setEvaluationId(evalId);
            ms.setModuleName(module.getModuleName());
            ms.setModuleTotal(module.getModuleTotal());
            ms.setModuleScore(module.getModuleScore());
            zntModuleScoreMapper.insertZntModuleScore(ms);
        }

        // 3. 插入步骤得分表
        for (StepScoreDTO step : request.getStepScores()) {
            ZntStepScore ss = new ZntStepScore();
            ss.setEvaluationId(evalId);
            ss.setStepName(step.getStepName());
            ss.setStepTotal(step.getStepTotal());
            ss.setStepScore(step.getStepScore());
            zntStepScoreMapper.insertZntStepScore(ss);
        }
    }

    @Override
    public EvaluationResponse findByChartUser(Long userId) {
        EvaluationResponse byChartUser = zntModuleScoreMapper.findByChartUser(userId);

        return byChartUser;
    }


}

